package config;

import com.alibaba.druid.pool.DruidDataSource;
import dao.DeptDao;
import dao.impl.DeptDaoImpl;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import service.DeptService;
import service.impl.DeptServiceImpl;

import javax.sql.DataSource;

@Configuration
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/demo");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return dataSource;
    }

    @Bean
    public QueryRunner queryRunner(DataSource dataSource) {
        QueryRunner queryRunner = new QueryRunner(dataSource, true);
        return queryRunner;
    }

    @Bean
    public DeptDao deptDao(QueryRunner queryRunner) {
        DeptDaoImpl dao = new DeptDaoImpl();
        dao.setQuery(queryRunner);
        return dao;
    }

    @Bean
    public DeptService deptService(DeptDao dao) {
        DeptServiceImpl deptService = new DeptServiceImpl();
        deptService.setDao(dao);//
        return deptService;
    }
}
